#!/bin/sh
#BK 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v407 fix for floppy drive icon.
#v409 gparted create ext3 part. failed, fixed by making /etc/mtab a symlink.
#120103 shinobar: need 'silent' option for ntfs-3g, avoid err msgs, permissions lost when copy.
#130203 change probedisk2 to probedisk.

#mount-FULL, umount-FULL, losetup-FULL are the full versions.
#The Busybox versions of mount and umount are available but only by:
# # busybox mount ...
# # busybox umount ...
#mount and umount are now scripts.
#if an ntfs partition, puppy uses user-mode ntfs-3g driver.
#the mount and umount scripts allow seamless mounting and unmounting of ntfs f.s.

[ ! $1 ] && exec busybox mount

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

if grep -q fido /etc/inittab ; then
	USER_OTPS="uid=1001,gid=500," #fido
	USER_OTPS_2="-o uid=1001,gid=500" #fido
fi

#i realised this script has to allow reentrancy. So, all temp file now unique,
#using ${$} which is pid of script.
MYPID=${$}

#v2.12 discovered difference between $@ and $*. Replaced all $@ with $* in this script...

#extract all the '-' options, on separate lines... do NOT use $@!!!!...
#v3.93 eliminate ' -- ' and all past it...
DASHOPTS="`echo "$*" | tr '\t' ' '  | sed -e 's/ -- .*//' | tr ' ' '\n' | grep '^\-'`"

case "$*" in
	*' vfat '*) VFAT_FLAG=yes ;;
	*' exfat '*) EXFAT_FLAG=yes ;;
	*' ntfs '*) NTFS_FLAG=yes ;;
	*' crypto_LUKS '*) LUKS_FLAG=yes ;;
esac

if [ "$VFAT_FLAG" = "yes" ];then
  case "$@" in
    *"-o "*) #don't override -o
      mount-FULL -n "${@}"
      ;;
    *)
      CMDPRMS="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/' | tr '\n' ' '`"
      NLS_PARAM=""
      if [ -f /etc/codepage ]; then
        grep -q -i '850' /etc/codepage && [ "$(echo $LANG|cut -d'.' -f1)" != "en_US" ] && NLS_PARAM=",codepage=850"
        grep -q -i '852' /etc/codepage && NLS_PARAM=",codepage=852,iocharset=iso8859-2"
      fi
      grep '^LANG=' /etc/profile | grep -q -i -E '\.utf|\.UTF' && NLS_PARAM="$NLS_PARAM,utf8"
      OPT="-o ${USER_OTPS}shortname=mixed,quiet${NLS_PARAM}"
      mount-FULL -t vfat ${OPT} -n $CMDPRMS
      ;;
  esac
  RETVAL=$?

elif [ "$EXFAT_FLAG" = "yes" ];then
  #older mount version: exfat systems fail with mount-FULL -n
  mount-FULL "${@}" || {
    # musl static mount.exfat fails
    #   however it works if calling the right app directly
    #   note: filemnt still fails. but it has extra code to make it work..
    wc=$(echo "${@}" | tr -s ' ' | wc -w)
    args=$(echo "$@" | cut -f "$(($wc-1))-" -d ' ')
    mount.exfat-fuse ${args}
  }
  RETVAL=$?

elif [ "$LUKS_FLAG" = "yes" ] ; then
  exec mount.crypto_LUKS "$@" #mount.crypto_LUKS has specific rox code

elif [ "$NTFS_FLAG" = "yes" ];then
  case "$@" in
    *"-o "*) #don't override -o
      ntfs-3g "${@}" || busybox mount "${@}"
      RETVAL=$?
      ;;
    *)
      #screen out all the options...
      CMDPRMS="`echo -n "$*" | tr '\t' ' ' | tr -s ' ' | tr ' ' '\n' | grep '^/' | tr '\n' ' '`"
      #kirk advised these options so Rox will not complain about file
      #permissions when copy a file to a ntfs partition...
      [ -f /tmp/ntfsmnterr${MYPID}.txt ] && rm -f /tmp/ntfsmnterr${MYPID}.txt
      ntfs-3g $CMDPRMS -o ${USER_OTPS}silent 2>/tmp/ntfsmnterr${MYPID}.txt
      RETVAL=$?
      #v2.16 ntfs-3g v1.417, part. scheduled for check, failed with value 10...
      #v4.00 ntfs-3g v1.2412 does not have 4,10, has 15 for dirty f.s., 14 hiberneted...
      case $RETVAL in 4|10|15) #14 = must mount read-only
        ntfs-3g $CMDPRMS -o ${USER_OTPS}force,silent 2>/tmp/ntfsmnterr${MYPID}.txt
        RETVAL=$?
        ERRMSG1="`cat /tmp/ntfsmnterr${MYPID}.txt`"
        echo "$ERRMSG1"
        if [ $RETVAL -eq 0 ];then
         echo "WARNING: NTFS f.s. mounted read/write but corrupted."
         [ ! "`pidof X`" = "" ] && nohup gxmessage -bg red -center -title "NTFS WARNING" "The ntfs-3g driver was able to mount the NTFS
partition but returned this error message:
$ERRMSG1

It is mounted read/write, but advice is only write
to it in emergency situation. Recommendation is
boot Windows and fix the filesystem first!!!" &
        fi
       [ ! -s /tmp/ntfsmnterr${MYPID}.txt ] && rm -f /tmp/ntfsmnterr${MYPID}.txt
       ;;
      esac
 
      #ntfs-3g plays very safe and will not mount if thinks anything
      #wrong with ntfs f.s. But, we may want to recover files from a
      #damaged windows. So, fall back to the kernel ntfs driver...
      if [ ! $RETVAL -eq 0 ];then
       #mount read-only...
       busybox mount -r -t ntfs $CMDPRMS
       RETVAL=$?
       ERRMSG1="`cat /tmp/ntfsmnterr${MYPID}.txt`"
       echo "$ERRMSG1"
       if [ $RETVAL -eq 0 ];then
        echo "WARNING: NTFS f.s. mounted read-only."
        [ ! "`pidof X`" = "" ] && nohup gxmessage -bg red -center -title "NTFS WARNING" "The ntfs-3g driver was unable to mount the NTFS
partition and returned this error message:
$ERRMSG1

So, the inbuilt kernel NTFS driver has been used
to mount the partition read-only." &
       fi
      fi
      ;;
  esac

### everything else ###
else
  #needs an explicit '-t ntfs', does not work with /etc/fstab...
  # busybox mount does not support '--bind'
  # as have mtab file (see below), can now use full mount...
  # v409 put in '-n' option as now have /etc/mtab symlink to /proc/mounts...
  mount-FULL -n "${@}" #SFR quotes
  RETVAL=$?
fi

if [ $RETVAL -eq 0 ] ; then
	if [ -f /usr/local/pup_event/frontend_funcs ] ; then
		#ROX: create or refresh desktop icon
		/usr/local/pup_event/frontend_funcs rox_mount "$DEVNAME"
	fi
fi

#v409, instead just make sure the symlink stays there...
if [ ! -L /etc/mtab ];then
 rm -f /etc/mtab
 ln -s /proc/mounts /etc/mtab
fi

exit $RETVAL
